package ${daoImplPackageName};

import ${daoPackageName}.${(domain.className)!}Dao;
import ${domianPackageName}.${domain.className};
import com.hzy.utils.MongoDBUtils;
import com.google.common.collect.Lists;
import com.platform.common.domain.to.PageTo;
import com.platform.pagehelper.Page;
import com.platform.pagehelper.PageHelper;
import com.platform.pagehelper.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

/**
* ${(domain.comment)!} dao实现类
*
* @author: ${domain.author}
* @version: 1.0
* @datetime: ${domain.datetime}
*/
@Repository
public class ${(domain.className)!}DaoImpl implements ${(domain.className)!}Dao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(${(domain.className)!} ${domain.lowerStartClassName}) throws Exception {
        ${domain.lowerStartClassName}.setCreateTime(System.currentTimeMillis());
        ${domain.lowerStartClassName}.setUpdateTime(System.currentTimeMillis());
    <#list domain.keys as item>
        ${domain.lowerStartClassName}.set${item.upperStartName}(null);
    </#list>
        mongoTemplate.insert(${domain.lowerStartClassName});
    }

    @Override
    public void update(${(domain.className)!} ${domain.lowerStartClassName}) throws Exception {
        //查询条件
    <#list domain.keys as item>
        Query query = new Query(Criteria.where("_id").is(${domain.lowerStartClassName}.get${item.upperStartName}()));
    </#list>
        //除主键外
        Update update= MongoDBUtils.getUpdateParams(${domain.lowerStartClassName});
        //更新查询返回结果集的第一条
        mongoTemplate.updateFirst(query,update,${(domain.className)!}.class);
    }

    @Override
    public void resort(${(domain.className)!} ${domain.lowerStartClassName}) throws Exception {
        String[] ids = ${domain.lowerStartClassName}.getIds().split(",");
        for(int i=0; i<ids.length; i++) {
            //过滤参数
            Query query = new Query(Criteria.where("_id").is(ids[i]));
            //修改排序
            Update update= new Update();
            update.set("sortNumber", i);
            mongoTemplate.updateFirst(query,update,${(domain.className)!}.class);
        }
    }

    @Override
    public ${(domain.className)!} findById(String ${domain.lowerStartClassName}Id) throws Exception {
        return mongoTemplate.findById(new ObjectId(${domain.lowerStartClassName}Id), ${(domain.className)!}.class);
    }

    @Override
    public List<${(domain.className)!}> find(${(domain.className)!} ${domain.lowerStartClassName}) throws Exception {
        Query query = MongoDBUtils.getQueryByParams(${domain.lowerStartClassName});
        query.with(Sort.by(Sort.Direction.DESC,"createTime"));
        return mongoTemplate.find(query,${(domain.className)!}.class);
    }

    @Override
    public PageInfo<${(domain.className)!}> findPage(${(domain.className)!} ${domain.lowerStartClassName}, PageTo pageTo) throws Exception {
        Query query = MongoDBUtils.getQueryByParams(${domain.lowerStartClassName});
        query.with(Sort.by(Sort.Direction.DESC,"createTime"));
        Page<${(domain.className)!}> page = PageHelper.startPage(pageTo.getPageNum(),pageTo.getPageSize());
        mongoTemplate.find(query,${(domain.className)!}.class);
        PageInfo<${(domain.className)!}> pageInfo = new PageInfo(page);
        return pageInfo;
    }

    @Override
    public void remove(${(domain.className)!} ${domain.lowerStartClassName}) throws Exception {
        List<String> ids = Lists.newArrayList(Arrays.asList(${domain.lowerStartClassName}.getIds().split(",")));
        if(ids.size() > 0){
            Query query=new Query(Criteria.where("_id").in(ids));
            mongoTemplate.remove(query, ${(domain.className)!}.class);
        }
    }

    @Override
    public void remove(String ${domain.lowerStartClassName}Id) throws Exception {
        Query query=new Query(Criteria.where("_id").is(${domain.lowerStartClassName}Id));
        mongoTemplate.remove(query, ${(domain.className)!}.class);
    }
}

